#include <Arduino.h>
#include <WiFi.h>
#include <AHT10.h>
#include <webota.h>
#include <U8g2lib.h>
#include <ico_img.h>

#define NTP1 "time1.cloud.tencent.com"
#define NTP2 "ntp1.aliyun.com"
#define NTP3 "ntp7.cnntp.org"

// U8G2_SSD1306_128X64_NONAME_F_HW_I2C oled(U8G2_R0, SCL, SDA, U8X8_PIN_NONE);   //0.96寸
U8G2_SH1106_128X64_NONAME_F_HW_I2C oled(U8G2_R0, SCL, SDA, U8X8_PIN_NONE); // 1.3寸

AHT10 aht20(AHT10_ADDRESS_0X38, AHT20_SENSOR);

uint8_t ad_pot = 34;

uint32_t timeout;
// uint32_t timenow;
uint32_t timehour;

void SmartConfig()
{
    WiFi.mode(WIFI_AP_STA);

    WiFi.beginSmartConfig(SC_TYPE_ESPTOUCH_AIRKISS); // 配网模式
    while (1)
    {
        if (WiFi.smartConfigDone())
        {
            break;
        }
    }
}

uint8_t AutoConfig()
{
    WiFi.begin();

    for (int i = 0; i < 20; i++)
    {
        int wstatus = WiFi.status();
        if (wstatus == WL_CONNECTED)
        {
            return 1;
        }
        else
        {
            delay(500);
        }
    }
    return 0;
}

void show_timer()
{

    struct tm timeInfo;
    if (!getLocalTime(&timeInfo))
    {
        return;
    }
    timeInfo.tm_year += 1900;

    oled.setCursor(0, 63);
    oled.println(timeInfo.tm_year);
    oled.println("-");
    oled.println(timeInfo.tm_mon + 1);
    oled.println("-");
    oled.println(timeInfo.tm_mday);
    oled.setCursor(83, 63);
    oled.println(week_d[timeInfo.tm_wday]);
    uint8_t hh = timeInfo.tm_hour;
    uint8_t mm = timeInfo.tm_min;
    uint8_t ss = timeInfo.tm_sec;
    oled.drawXBM(127 - 16 * 5 - 6, 17, 16, 32, clock_16x32[hh / 10]);
    oled.drawXBM(127 - 16 * 4 - 6, 17, 16, 32, clock_16x32[hh % 10]);
    oled.drawXBM(127 - 16 * 3 - 6, 17, 6, 32, mh_6x32);
    oled.drawXBM(127 - 16 * 3, 17, 16, 32, clock_16x32[mm / 10]);
    oled.drawXBM(127 - 16 * 2, 17, 16, 32, clock_16x32[mm % 10]);
    oled.setCursor(127 - 16, 47);
    oled.println(ss / 10);
    oled.setCursor(127 - 8, 47);
    oled.println(ss % 10);
}

void show_dianchi(uint8_t adport)
{
    uint16_t R1 = 20000;
    uint16_t R2 = 51000;
    uint16_t vcc = (32700000 / R2) * (R1 + R2) / 100000;
    uint16_t ad_value = (analogRead(adport) * 1000 / 4095) * vcc / 1000;
    ad_value = 420;
    if (ad_value >= 410)
    {
        oled.drawXBM(127 - 16, 0, 16, 16, battery_16x16[5]);
    }
    if (ad_value >= 380 && ad_value < 410)
    {
        oled.drawXBM(127 - 16, 0, 16, 16, battery_16x16[4]);
    }
    if (ad_value >= 350 && ad_value < 380)
    {
        oled.drawXBM(127 - 16, 0, 16, 16, battery_16x16[3]);
    }
    if (ad_value >= 320 && ad_value < 350)
    {
        oled.drawXBM(127 - 16, 0, 16, 16, battery_16x16[2]);
    }
    if (ad_value >= 290 && ad_value < 320)
    {
        oled.drawXBM(127 - 16, 0, 16, 16, battery_16x16[1]);
    }
    if (ad_value < 290)
    {
        oled.drawXBM(127 - 16, 0, 16, 16, battery_16x16[0]);
    }
}

// wifi信号显示
void show_LET(uint8_t ssid_w)
{
    if (ssid_w >= 50)
    {
        oled.drawXBM(0, 0, 16, 16, LET_16x16[5]);
    }
    if (ssid_w >= 40 && ssid_w < 50)
    {
        oled.drawXBM(0, 0, 16, 16, LET_16x16[4]);
    }
    if (ssid_w >= 30 && ssid_w < 40)
    {
        oled.drawXBM(0, 0, 16, 16, LET_16x16[3]);
    }
    if (ssid_w >= 20 && ssid_w < 30)
    {
        oled.drawXBM(0, 0, 16, 16, LET_16x16[2]);
    }
    if (ssid_w >= 10 && ssid_w < 20)
    {
        oled.drawXBM(0, 0, 16, 16, LET_16x16[1]);
    }
    if (ssid_w < 10)
    {
        oled.drawXBM(0, 0, 16, 16, LET_16x16[0]);
    }
}

void show_AHT20()
{
    oled.setCursor(0, 31);
    oled.println((int)aht20.readTemperature()); // 温度
    oled.println("°C");
    oled.setCursor(0, 47);
    oled.println((int)aht20.readHumidity()); // 湿度
    oled.println("%");
}

void setup()
{
    Serial.begin(115200);
    oled.begin();
    // WiFi.eraseAP();
    oled.enableUTF8Print();
    oled.setFont(u8g2_font_wqy15_t_gb2312);
    oled.drawUTF8(0, 15, "正在连接WIFI");
    oled.sendBuffer();
    if (!AutoConfig())
    {
        oled.clearBuffer();
        oled.drawUTF8(0, 15, "打开微信小程序");
        oled.drawUTF8(0, 31, "一键配网");
        oled.drawUTF8(0, 47, "配置WiFi连接");
        oled.drawUTF8(0, 63, "仅支持2.4G网络");
        oled.sendBuffer();
        SmartConfig();
    }
    oled.clearBuffer();
    oled.drawUTF8(0, 15, "WIFI已连接");
    oled.drawUTF8(0, 31, WiFi.SSID().c_str());
    oled.drawUTF8(0, 47, WiFi.psk().c_str());
    oled.setCursor(0, 63);
    oled.println(WiFi.localIP());
    oled.sendBuffer();
    configTime(8 * 3600, 0, NTP1, NTP2, NTP3);
    Web_OTA_start();
    aht20.begin();
    pinMode(ad_pot, INPUT); // ADC 电池电量检测引脚
}

void loop()
{
    if (WiFi.status() == WL_CONNECTED)
    {
        // 网络已连接
        Web_OTA_server();
        if (millis() - timehour > 3600000)
        {
            timehour = millis();
            configTime(8 * 3600, 0, NTP1, NTP2, NTP3);
        }
    }
    else
    {
        if (millis() - timeout > 1000)
        {
            timeout = millis();
            WiFi.begin();
        }
    }

    if (millis() - timeout > 1000)
    {
        timeout = millis();
        oled.clearBuffer();
        oled.drawXBM(16, 0, 16, 16, ico_msg);      /*信息*/
        oled.drawXBM(34, 0, 8, 16, ico_lightning); /*闪电*/
        oled.drawXBM(44, 0, 8, 16, ico_sound);     /*喇叭*/
        oled.drawXBM(54, 0, 8, 16, ico_blue);      /*蓝牙*/

        show_timer(); // 日期时间
        show_AHT20();
        show_LET(100 + WiFi.RSSI()); // 信号
        show_dianchi(ad_pot);        // 电量
        oled.sendBuffer();
    }
}
